package com.m3839.sdk.common.util;

import android.text.TextUtils;

import java.util.regex.Pattern;

/**
 * Description:StringUtils
 *
 * @author 陈宝阳
 * @create 2020/9/2 13:48
 */
public class StringUtils {

  private static final String HTTP_URL_REGEX = "^((ht|f)tps?):\\/\\/([\\w\\-]+(\\.[\\w\\-]+)*\\/)*[\\w\\-]+(\\.[\\w\\-]+)*\\/?(\\?([\\w\\-\\.,@?^=%&:\\/~\\+#]*)+)?";

  private static final String EQUAL_SIGN = "=";

  private static final String COMMA = ",";

  public static boolean isEmpty(String str) {
    return str == null || str.length() == 0;
  }

  /**
   * 校验url是否是合法的地址
   * @param url
   * @return
   */
  public static boolean isValidHttpUrl(String url) {
    if(TextUtils.isEmpty(url)) {
      return false;
    }
    Pattern pattern = Pattern.compile(HTTP_URL_REGEX);
    return pattern.matcher(url).matches();
  }

  public static String formatString(String... text) {
    StringBuilder msgBuilder = new StringBuilder();
    for (int i = 0; i < text.length; i++) {
      msgBuilder.append(text[i]);
      if(i%2 == 0) {
        msgBuilder.append(text[i]);
        msgBuilder.append(EQUAL_SIGN);
      }else {
        msgBuilder.append(text[i]);
        if(i == text.length - 1) {
          break;
        }
        msgBuilder.append(COMMA);
      }
    }
    return msgBuilder.toString();
  }
}
